// 抽象工厂
public interface ComputerFactory {
    Car createCar();
    Engine createEngine();
}

// 具体工厂之一
public class BenzFactory implements ComputerFactory{
    @Override
    public Car createCar(){
        return new MercedesBenzCar();
    }
    
    @Override
    public Engine createEngine(){
        return new MercedesBenzEngine();
    }
}

// 另一个具体工厂
public class BMWFactory implements ComputerFactory{
    @Override
    public Car createCar(){
        return new BMWCars();
    }
        
    @Override
    public Engine createEngine(){
        return new BMWEngine();
    }
}

// 产品A - 车辆的公共接口
public interface Car {
  void show();
}

// 苯兹车辆实现
public class MercedesBenzCar implements Car {
   @Override 
   public void show(){
       System.out.println("This is a benz car");
   }
}

//宝马车辆实现
public class BMWCars implements Car {
   @Override  
   public void show(){
      System.out.println("This is a bmw car");
   }
}

// 产品B - 引擎的公共接口   
public interface Engine {
  void show();
}

//苯兹引擎实现
public class MercedesBenzEngine implements Engine {
	@Override    
     public void show(){
         System.out.println("This is a benz engine");
     }
}

// 宝马引擎实现
public class BMWEngine implements Engine {
	@Override     
     public void show(){
          System.out.println("This is a bmw engine");
     }
}

// 生产者
public class FactoryProducer {

    public static ComputerFactory getFactory(String choice){
        if(choice == null)
            return null;

        else if (choice.equalsIgnoreCase("benz"))
           return new BenzFactory();

        else if (choice.equalsIgnoreCase("bmw"))        
           return new BMWFactory();

        return null;
    }
}

// 客户端测试
public class AbstractFactoryPatternDemo {
    public static void main(String[] args) {
        ComputerFactory factory = FactoryProducer.getFactory("benz");

        Car car = factory.createCar();
        car.show();

        Engine engine = factory.createEngine();
        engine.show();

        factory = FactoryProducer.getFactory("bmw"12);

        car = factory.createCar();
        car.show();

        engine = factory.createEngine();
        engine.show();
    }
}